clear all
tempfile tempsave
set seed ${seed}


// -----------------------------------------------------
// Table 2
// -----------------------------------------------------
use "${data}data_estim.dta" if sample2==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears c.pgs_edu 	///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t2_1", replace

reghdfe eduyears c.pgs_edu ///
$controls [pw=w0], absorb(famid) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t2_2", replace

qui{
	reg eduyears c.pgs_edu  	///
	$controls i.w1state if e(sample)
	eststo bw

	destring famid, replace
	reg eduyears c.pgs_edu  	///
	$controls i.famid if e(sample)
	eststo wi

	suest bw wi, vce(cluster HsId)
	noi: lincom [bw_mean]pgs_edu-[wi_mean]pgs_edu

	estimates use "${results}t2_2"
	estadd scalar d_b=r(estimate)
	estadd scalar d_p=r(p)
	estadd scalar d_se=r(se)
	estadd scalar d_ub=r(ub)
	estadd scalar d_lb=r(lb)
	est save "${results}t2_2", replace
}

use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe pred c.f_over1 ///
[pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t2_3", replace

reghdfe pred c.f_over1 ///
$peer [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t2_4", replace

qui{
	reg pred c.f_over1 	///
	i.w1state if e(sample)
	eststo bw

	reg pred c.f_over1 	///
	$peer i.w1state if e(sample)
	eststo wi

	noi: suest bw wi, vce(cluster HsId)
	noi: lincom [bw_mean]f_over1 - [wi_mean]f_over1

	estimates use "${results}t2_4"
	estadd scalar d_b=r(estimate)
	estadd scalar d_p=r(p)
	estadd scalar d_se=r(se)
	estadd scalar d_ub=r(ub)
	estadd scalar d_lb=r(lb)
	est save "${results}t2_4", replace

}

// -----------------------------------------------------
// Table 3
// -----------------------------------------------------
reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_1", replace

reghdfe eduyears c.pgs_edu##(c.f_over2)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_2", replace

reghdfe eduyears c.pgs_edu##(c.teach_master)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_3", replace

reghdfe eduyears c.pgs_edu##(c.teach_5years)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_4", replace

reghdfe eduyears c.pgs_edu##(c.teach_new)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_5", replace

reghdfe eduyears c.pgs_edu##(c.class)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t3_6", replace

// -----------------------------------------------------
// Table 4
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears (c.pgs_edu)##(c.f_over1) ///
$controls (c.pgs_edu c.f_over1)#($controls)  [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t5_1", replace

reghdfe eduyears (c.pgs_edu)##(c.f_over1) ///
(c.f_over1##c.f_over1)##(c.pgs_edu##c.pgs_edu) ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t5_2", replace

use "${data}data_estim.dta" if sample5==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls 														[pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t5_3", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls pvt_lag_res grade_sci_res grade_mat_res  				[pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t5_4", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls c.(pvt_lag_res grade_sci_res grade_mat_res)##(c.f_over1) [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t5_5", replace

// -----------------------------------------------------
// Table 5
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

local m=1
foreach r in $school{
	di "School Characteristic: `r'"
	replace RobSchlOther=`r'
	reghdfe eduyears (c.pgs_edu)##(c.f_over1 c.RobSchlOther) ///
	$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}t4_`m'", replace
	local ++m
}

// School FE
reghdfe eduyears (c.pgs_edu)##(c.f_over1) ///
$controls [pw=w0], absorb(HsId) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t4_`m'", replace

// -----------------------------------------------------
// Table 6
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears (c.pgs_edu)##(c.f_over1) ///
$controls c.inv c.auth2 [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_5", replace

reghdfe eduyears (c.pgs_edu)##(c.f_over1) ///
$controls c.inv c.auth2 (c.inv c.auth2)#(c.pgs_edu c.f_over1) [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_6", replace

reghdfe c.inv (c.pgs_edu)##(c.f_over1) ///
$controls [pw=w0]  , cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_1", replace

reghdfe c.auth2 (c.pgs_edu)##(c.f_over1) ///
$controls [pw=w0]  , cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_3", replace

use "${data}data_estim.dta" if sample2==1, clear
qui: gstats transform (standardize)	$s_vars pgs_sib [aw=w0] , replace

sum inv pgs_sib auth2

reghdfe c.inv (c.pgs_edu)##(c.f_over1) c.pgs_sib ///
$controls [pw=w0], cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_2", replace

reghdfe c.auth2 (c.pgs_edu)##(c.f_over1) c.pgs_sib ///
$controls [pw=w0], cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}t6_4", replace


// -----------------------------------------------------
// Table 7
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

local m=1
foreach r in $skills {
	di "Skill: `r'"
	reghdfe `r' (c.pgs_edu)##(c.f_over1) ///
	$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}t7_`m'", replace
	local ++m
}

// -----------------------------------------------------
// Table S.1
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

local m=1
foreach x in f_over1 $cpeer{

	reghdfe pgs_edu `x'  [pw=w0], noabsorb cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts1_`m'", replace
	local ++m


	reghdfe pgs_edu `x' $controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts1_`m'", replace
	local ++m

}

// -----------------------------------------------------
// Table S.2
// -----------------------------------------------------
use "${data}data_estim.dta" if sample6==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts2_1", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls_r $tpeer [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts2_2", replace

// -----------------------------------------------------
// Table S.3
// -----------------------------------------------------
*Reweighting
use "${data}data_estim.dta" if sample1==1, clear
merge 1:1 aid using "${temp}reweight.dta", keepusing(w1)
qui: gstats transform (standardize)	$s_vars [aw=w1] , replace

reghdfe eduyears c.pgs_edu##(c.f_over1) ///
$controls [pw=w1], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts3_1", replace

*Lenient Imputation
use "${data}data_estim.dta" if sample3==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears c.pgs_edu##(c.f_over1) ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts3_2", replace

*Stringent Imputation
use "${data}data_estim.dta" if sample4==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

reghdfe eduyears c.pgs_edu##(c.f_over1) ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts3_3", replace

// -----------------------------------------------------
// Table S.5
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

local m=1
foreach x of varlist teach_master teach_5years teach_new class{

	reghdfe pred c.`x' ///
	[pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts5_`m'", replace
	local ++m


	reghdfe pred  c.`x' ///
	$peer [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts5_`m'", replace

	qui{
		reg pred c.`x' 	///
		i.w1state if e(sample)
		eststo bw

		reg pred c.`x' 	///
		$peer i.w1state if e(sample)
		eststo wi

		noi: suest bw wi, vce(cluster HsId)
		noi: lincom [bw_mean]`x' - [wi_mean]`x'

		estimates use "${results}ts5_`m'"
		estadd scalar d_b=r(estimate)
		estadd scalar d_p=r(p)
		estadd scalar d_se=r(se)
		estadd scalar d_ub=r(ub)
		estadd scalar d_lb=r(lb)
		est save "${results}ts5_`m'", replace

	}
	local ++m
}

// -----------------------------------------------------
// Table S.6
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace


reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
[pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts6_1", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
c.gender##c.age_home_w1 c.(pc1-pc20) c.firstborn [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts6_2", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
c.gender##c.age_home_w1 c.(pc1-pc20) c.firstborn $family [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts6_3", replace

reghdfe eduyears c.pgs_edu##(c.f_over1)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts6_4", replace

// -----------------------------------------------------
// Table S.7
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace
reghdfe eduyears c.pgs_edu##(c.f_extend1)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts7_1", replace

reghdfe eduyears c.pgs_edu##(c.f_extend2)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts7_2", replace

reghdfe eduyears c.pgs_edu##(c.f_extend3)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts7_3", replace

reghdfe eduyears c.pgs_edu##(c.f_extend4)  ///
$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
qui estat summarize
qui estadd matrix stats=r(stats)
est save "${results}ts7_4", replace

// -----------------------------------------------------
// Table S.8
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

local m=1
foreach r in $pgsoth {
	replace RobGeneOther=`r'
	reghdfe eduyears (c.pgs_edu c.RobGeneOther)##(c.f_over1) ///
	$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts8_`m'", replace
	local ++m
}

// -----------------------------------------------------
// Table S.10
// -----------------------------------------------------
use "${data}data_estim.dta" if sample7==1, clear
qui: gstats transform (standardize)	$s_vars  [aw=w0] , replace

local m=1
foreach r in pvt_lag risk2 pat2 health_sub1 pvt risk pat health_sub{
	di "Skill: `r'"
	reghdfe `r' (c.pgs_edu)##(c.f_over1) ///
	$controls [pw=w0], absorb(w1state) cluster(HsId) keepmata
	qui estat summarize
	qui estadd matrix stats=r(stats)
	est save "${results}ts10_`m'", replace
	local ++m
}


// -----------------------------------------------------
// Table S.9
// -----------------------------------------------------
use "${data}data_estim.dta" if sample1==1, clear
qui: gstats transform (standardize)	$s_vars [aw=w0] , replace

// foreach x of varlist $s_vars{
// 	su `x' [aw=w0]
// 	replace `x'=(`x'-r(mean))/r(sd)
// } 

save "C:\Users\sn21763\OneDrive - University of Bristol\Desktop\rep.dta", replace



// create variables
tab w1state, gen(w1state_)
drop w1state_1

gen gxage=gender*age_home_w1

keep aid scid eduyears pgs_edu f_over1 $child $family $peer w1state_* gxage

// export data
export delimited "${temp}pgi_correct.txt", delim(" ") replace
